<template>
  <div>
    <el-form :inline="true" class="demo-form-inline" :model="formId">
      <el-form-item label="一级分类">
        <el-select
          placeholder="请选择"
          v-model="formId.category1Id"
          @change="handler1"
          :disabled="show"
        >
          <el-option
            :label="c1.name"
            :value="c1.id"
            v-for="c1 in list1"
            :key="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择"
          v-model="formId.category2Id"
          @change="handler2"
          :disabled="show"
        >
          <el-option
            :label="c2.name"
            :value="c2.id"
            v-for="c2 in list2"
            :key="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          placeholder="请选择"
          v-model="formId.category3Id"
          @change="handler3"
          :disabled="show"
        >
          <el-option
            :label="c3.name"
            :value="c3.id"
            v-for="c3 in list3"
            :key="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelect",
  props: ["show"],
  data() {
    return {
      // 一级分类数据
      list1: [],
      // 二级分类数据
      list2: [],
      list3: [],
      // 收集相应的id
      formId: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
    };
  },
  mounted() {
    // 获取一级分类的数据
    this.getCategory1List();
  },
  methods: {
    async getCategory1List() {
      let result = await this.$api.reqCategory1List();
      // console.log(result);
      if (result.code === 200) {
        this.list1 = result.data;
      }
    },
    // 通过一级分类获取二级分类数据
    async handler1() {
      // 清除数据
      // this.list2 = [];
      this.list3 = [];
      this.formId.category2Id = "";
      this.formId.category3Id = "";
      const { category1Id, category2Id, category3Id } = this.formId;
      this.$emit("getCategoryId", { categoryId: category1Id, level: 1 });
      // this.$emit("getCategoryId", { categoryId: category2Id, level: 2 });
      // this.$emit("getCategoryId", { categoryId: category3Id, level: 3 });
      let result = await this.$api.reqCategory2List(category1Id);
      // console.log(result);
      if (result.code === 200) {
        this.list2 = result.data;
      }
    },
    // 通过二级分类获取三级分类数据
    async handler2() {
      // 清除数据
      // this.list3 = [];
      this.formId.category3Id = "";
      const { category2Id, category3Id } = this.formId;
      this.$emit("getCategoryId", { categoryId: category2Id, level: 2 });
      // this.$emit("getCategoryId", { categoryId: category3Id, level: 3 });
      let result = await this.$api.reqCategory3List(category2Id);
      // console.log(result);
      if (result.code === 200) {
        this.list3 = result.data;
      }
    },
    // 通过三级分类获取平台属性列表
    handler3() {
      const { category3Id } = this.formId;
      this.$emit("getCategoryId", { categoryId: category3Id, level: 3 });
    },
  },
};
</script>

<style scoped>
</style>